package com.wyp168.juc.future;

import com.wyp168.juc.util.ExecutorsUtil;

import java.util.concurrent.*;

/**
 * @ClassName CompletableFutureRunMethodDemo001
 * @Description TODO
 * @Author wyp168
 * @Date 2022/9/12 19:40
 */
public class CompletableFutureRunMethodDemo001   {
    public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException {
        ThreadPoolExecutor threadPool = ExecutorsUtil.getThreadPoolExecutor();

        CompletableFuture<Void> completableFuture = CompletableFuture.supplyAsync(() -> {
//                    try {
//                        TimeUnit.MILLISECONDS.sleep(20);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
                    System.out.println("任务一: " + Thread.currentThread().getName());
                    return "abc";
                }, threadPool)
                .thenRun(() -> {
                    try {
                        TimeUnit.MILLISECONDS.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("任务二：" + Thread.currentThread().getName());
                })
                .thenRun(() -> {
                    try {
                        TimeUnit.MILLISECONDS.sleep(30);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("任务三：" + Thread.currentThread().getName());
                })
                .thenRun(() -> {
                    try {
                        TimeUnit.MILLISECONDS.sleep(20);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("任务四：" + Thread.currentThread().getName());
                });
        threadPool.shutdown();
        System.out.println(completableFuture.get(3, TimeUnit.SECONDS));
        System.out.println("Main thread has done!");
    }
}
